unit SmallStruct.View.TSSChartView;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  SmallStruct.View.TSSDataView,
  cxContainer,
  cxEdit,
  ExtCtrls,
  RzPanel,
  TeeProcs,
  TeEngine,
  Chart,
  DbChart,
  DB,
  Series;

type
  TSSChartView = class(TSSDataView)
    _Chart: TDBChart;
  private
    procedure AppendBarSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const BarColor: TColor);
    procedure AppendPieSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const PieColor: TColor);
    procedure AppendFastLineSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const FastLineColor: TColor);
    procedure AppendPointSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const FastLineColor: TColor);
  public
    procedure InitObject; override;
  end;

var
  SSChartView: TSSChartView;

implementation

uses SmallStruct.Configuration,
  SmallStruct.Model;

{$R *.dfm}

{ TSSChartView }

procedure TSSChartView.AppendBarSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const BarColor: TColor);
var
  BarSeries: TBarSeries;
begin
  BarSeries := TBarSeries.Create(Self);
  BarSeries.Title := ValueFieldName;
  with BarSeries do
  begin
    Marks.Visible := True;
    XValues.Name := 'X';
    YValues.Name := 'Bar';
    YValues.Order := loNone;
  end;

  while not DataSet.Eof do
  begin
    BarSeries.AddBar(DataSet.FieldByName(ValueFieldName).AsFloat, DataSet.FieldByName(LableFieldName).AsString, BarColor);
    DataSet.Next;
  end;

  _Chart.AddSeries(BarSeries);
end;

procedure TSSChartView.AppendFastLineSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const FastLineColor: TColor);
var
  FastLineSeries: TLineSeries;
begin
  FastLineSeries := TLineSeries.Create(Self);
  FastLineSeries.Title := ValueFieldName;

  with FastLineSeries do
  begin
    ColorEachPoint := True;
    Marks.Visible := False;
    Pointer.InflateMargins := True;
    Pointer.Style := psRectangle;
    Pointer.Visible := True;
    XValues.Name := 'X';
    XValues.Order := loAscending;
    YValues.Name := 'Y';
    YValues.Order := loNone;
  end;

  while not DataSet.Eof do
  begin
    FastLineSeries.Add(DataSet.FieldByName(ValueFieldName).AsFloat, DataSet.FieldByName(LableFieldName).AsString);
    DataSet.Next;
  end;

  _Chart.AddSeries(FastLineSeries);
end;

procedure TSSChartView.AppendPieSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const PieColor: TColor);
var
  PieSeries: TPieSeries;
begin
  PieSeries := TPieSeries.Create(Self);
  PieSeries.Title := ValueFieldName;

  with PieSeries do
  begin
    Marks.Visible := True;
    YValues.Name := 'Pie';
    YValues.Order := loNone;
    Circled := True;
  end;

  while not DataSet.Eof do
  begin
    PieSeries.AddPie(DataSet.FieldByName(ValueFieldName).AsFloat, DataSet.FieldByName(LableFieldName).AsString, PieColor);
    DataSet.Next;
  end;

  _Chart.AddSeries(PieSeries);
end;

procedure TSSChartView.AppendPointSeries(DataSet: TDataSet; const LableFieldName, ValueFieldName: string; const FastLineColor: TColor);
var
  PointSeries: TPointSeries;
begin
  PointSeries := TPointSeries.Create(Self);
  PointSeries.Title := ValueFieldName;

  with PointSeries do
  begin
    Marks.Visible := False;
    ClickableLine := False;
    Pointer.InflateMargins := True;
    Pointer.Style := psDiamond;
    Pointer.Visible := True;
    XValues.Name := 'X';
    XValues.Order := loAscending;
    YValues.Name := 'Y';
    YValues.Order := loNone;
  end;

  while not DataSet.Eof do
  begin
    PointSeries.Add(DataSet.FieldByName(ValueFieldName).AsFloat, DataSet.FieldByName(LableFieldName).AsString);
    DataSet.Next;
  end;

  _Chart.AddSeries(PointSeries);
end;

procedure TSSChartView.InitObject;
var
  ViewConfiguration: ISSViewConfiguration;
  FieldConfiguration: ISSViewFieldConfiguration;
  Index: Integer;
  LableFieldName: string;
  ValueFieldNames: TStringList;
  DataSet: TDataSet;
begin
  inherited;
  ViewConfiguration := GetContext.GetConfiguration as ISSViewConfiguration;
  ValueFieldNames := TStringList.Create;

  DataSet := (GetPrimaryModel as ISSDataModel).GetDataSet.GetDataSet;

  for Index := 0 to ViewConfiguration.GetFieldConfiguration.GetCount - 1 do
  begin
    FieldConfiguration := ViewConfiguration.GetFieldConfiguration.GetObject(Index) as ISSViewFieldConfiguration;
    if FieldConfiguration.GetType = ssvftChartLable then
      LableFieldName := FieldConfiguration.GetFieldName;
    if FieldConfiguration.GetType = ssvftChartValue then
      ValueFieldNames.Append(FieldConfiguration.GetFieldName);
  end;

  for Index := 0 to ValueFieldNames.Count - 1 do
  begin
    case ViewConfiguration.GetType of
      ssvtBarChartView: AppendBarSeries(DataSet, LableFieldName, ValueFieldNames.Strings[Index], clBlue);
      ssvtPieChartView: AppendPieSeries(DataSet, LableFieldName, ValueFieldNames.Strings[Index], clBlue);
      ssvtFastLineChartView: AppendFastLineSeries(DataSet, LableFieldName, ValueFieldNames.Strings[Index], clBlue);
      ssvtPointChartView: AppendPointSeries(DataSet, LableFieldName, ValueFieldNames.Strings[Index], clBlue);
    end;
  end;
end;

end.
